package P150;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author LiangMu
 * @version \$Id: P45.java, v 0.1, 2024年07月06日 02:44 LiangMu Exp $$
 */
public class P45 {

    public List<String> summaryRanges(int[] nums) {
        if (nums.length == 0) {
            return Collections.emptyList();
        }
        List<String> res = new ArrayList<>();
        int left = nums[0], right = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] - nums[i - 1] != 1) {
                if (left == right) {
                    res.add(left + "");
                } else {
                    res.add(left + "->" + right);
                }
                left = right = nums[i];
            } else {
                right = nums[i];
            }
        }
        if (left == right) {
            res.add(left + "");
        } else {
            res.add(left + "->" + right);
        }
        return res;
    }

}
